/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-11-24 23:34:52
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.bp.repository;

import com.joysuch.wwyt.bp.entity.BpProductionFacility;
import com.joysuch.wwyt.dataperm.annotation.DisableDataPermission;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

import java.util.Date;
import java.util.List;

/**
 * BpProductionFacilityDao
 */
public interface BpProductionFacilityDao
        extends JpaRepository<BpProductionFacility, Long>, QuerydslPredicateExecutor<BpProductionFacility> {
    public Page<BpProductionFacility> findAll(Specification<BpProductionFacility> spec, Pageable pageable);

    public BpProductionFacility save(BpProductionFacility bean);

    public void delete(BpProductionFacility bean);

    public BpProductionFacility findByCode(String code);

    @Query(value = "select  * from wwyt_bp_production_facility t where  t.code =?1 and org_code like ?2% and tenent_id=1 limit 1", nativeQuery = true)
    public BpProductionFacility findByCodeAndCompanyCode(String code, String companyCode);

    @Query("select d.name from BpProductionFacility p, BpDepart d where p.departId = d.id and p.id = ?1")
    String findDepartNameById(Long id);

    @Modifying
    @Query("update BpProductionFacility p set type = null where p.type = ?1")
    void resetTypeId(Long typeId);

    List<BpProductionFacility> findByType(Long type);

    @Modifying
    @Query("update BpProductionFacility p set repairState = ?2 where p.id = ?1")
    public void updateReapairState(Long id, int state);
    @Modifying
    @Query("update BpProductionFacility p set p.state = ?1 where p.id in (?2)")
    public void updateState(int state,List<Long> ids);

    @Query("select m from BpProductionFacility m where m.maintainPeriodDays is not null and m.maintainPeriodDays > 0 and m.lifecycle != ?1")
    public List<BpProductionFacility> findNeedMaintainFailicities(String excludeLifecyle);


    int countByCode(String code);

    @DisableDataPermission
    List<BpProductionFacility> getByCode(String code);

    @Query(value = "SELECT * FROM wwyt_bp_production_facility WHERE id IN(" +
            "SELECT DISTINCT(DEVICE_FACILITY_ID) FROM wwyt_bp_device_facility_repair_record WHERE delete_flag = '0' AND REPAIR_END_DATE >?1) " +
            "AND delete_flag = '0'", nativeQuery = true)
    List<BpProductionFacility> getNeedRepairCheckFacility(Date beforeDate);

    @Query(value = "SELECT * FROM wwyt_bp_production_facility WHERE id IN(" +
            "SELECT DISTINCT(DEVICE_FACILITY_ID) FROM wwyt_bp_device_facility_maintain_record WHERE delete_flag = '0' AND maintain_end_date >?1) " +
            "AND delete_flag = '0'", nativeQuery = true)
    List<BpProductionFacility> getNeedMaintainCheckFacility(Date beforeDate);


    @Query(value = "SELECT f.* FROM wwyt_bp_production_facility f INNER JOIN wwyt_bp_device_facility_stop_recover_record r " +
            "ON f.id = r.DEVICE_FACILITY_ID " +
            "WHERE f.delete_flag = '0' AND f.delete_flag = '0'AND r.id = ?1", nativeQuery = true)
    BpProductionFacility getOneFromStopRecoverRecord(Long recordId);

    @Query(value = " SELECT f.* FROM wwyt_bp_production_facility f where f.DELETE_FLAG = '0' and f.LOCATION is not null  " , nativeQuery = true)
    List<BpProductionFacility> findAllHaveLocation();


    @Query(value = " SELECT f.id FROM wwyt_bp_production_facility f where f.DELETE_FLAG = '0' and f.MAJOR_HAZARD_ID =?1 " , nativeQuery = true)
    List<Long> findByMajorHazardId(Long id);
}
